package com.cat.model.web.action.api;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.util.TimeUtil;
import com.cat.model.biz.service.api.BasOrderService;
import com.cat.model.common.model.common.ModelExtendtimeVO;
import com.cat.model.common.model.common.UserTokenVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.dao.mapper.UserInfoMapper;
import com.cat.model.web.action.base.BaseAction;

public class BasOrderAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 604080577375901309L;
	
	private String token;
	
	private Long modelId;//模特Id
	private String workDate;//工作日期
	private Integer addWorkHours;//加时工作小时数
	private BigDecimal unitAmount;//单价
	private BigDecimal totalAmount;//总金额
	private String refOrderNumber;//原订单号
	
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private BasOrderService basOrderService;
	
	
	/**
	 * 生成模特加时订单
	 * */
	public void createModelExtendtime(){
		if(StringUtils.isEmpty(token)||StringUtils.isEmpty(refOrderNumber)||modelId==null||unitAmount==null||totalAmount==null||addWorkHours==null||workDate==null){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		ModelExtendtimeVO extendtime = new ModelExtendtimeVO();
		extendtime.setUnitAmount(unitAmount);
		extendtime.setTotalAmount(totalAmount);
		extendtime.setRefOrderNumber(refOrderNumber);
		Date workDateTime = TimeUtil.parse("yyyy-MM-dd", workDate);
		extendtime.setWorkDate(workDateTime);
		extendtime.setAddWorkHours(addWorkHours);
		extendtime.setModelId(modelId);
		
		print(basOrderService.createModelExtendtimeOrder(userToken.getUserId(), extendtime));
	}
	
	/**
	 * 订单退款申请
	 * */
	public void revorkBasOrder(){
		if(StringUtils.isEmpty(token)||StringUtils.isEmpty(refOrderNumber)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(basOrderService.revorkBasOrder(userToken.getUserId(), refOrderNumber));
	}


	/**
	 * 根据订单号获取订单详情
	 * */
	public void getOrderDetail(){
		if(StringUtils.isEmpty(token)||StringUtils.isEmpty(refOrderNumber)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(basOrderService.getOrderDetail(userToken.getUserId(), refOrderNumber));
	}
	
	/**
	 * 根据订单号获取订单状态
	 * */
	public void getOrderStatus(){
		if(StringUtils.isEmpty(token)||StringUtils.isEmpty(refOrderNumber)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(basOrderService.getOrderStatus(userToken.getUserId(), refOrderNumber));
	}
	public String getToken() {
		return token;
	}


	public void setToken(String token) {
		this.token = token;
	}


	public Long getModelId() {
		return modelId;
	}


	public void setModelId(Long modelId) {
		this.modelId = modelId;
	}


	public String getWorkDate() {
		return workDate;
	}


	public void setWorkDate(String workDate) {
		this.workDate = workDate;
	}


	public Integer getAddWorkHours() {
		return addWorkHours;
	}


	public void setAddWorkHours(Integer addWorkHours) {
		this.addWorkHours = addWorkHours;
	}


	public BigDecimal getUnitAmount() {
		return unitAmount;
	}


	public void setUnitAmount(BigDecimal unitAmount) {
		this.unitAmount = unitAmount;
	}


	public BigDecimal getTotalAmount() {
		return totalAmount;
	}


	public void setTotalAmount(BigDecimal totalAmount) {
		this.totalAmount = totalAmount;
	}


	public String getRefOrderNumber() {
		return refOrderNumber;
	}


	public void setRefOrderNumber(String refOrderNumber) {
		this.refOrderNumber = refOrderNumber;
	}

}
